<template>
    <div>
        <div style="padding: 1px 0">
          <el-input
            style="width: 200px; font-size:medium"
            prefix-icon="el-icon-search"
            placeholder="请输入角色名"
            v-model="name"
          ></el-input>
          <el-button class="ml-5" type="primary" @click="load" round style="font-size:medium">搜索</el-button>
          <el-button class="ml-5" type="warning" @click="reset" round style="font-size:medium">重置</el-button>
        </div>

        <div style="padding: 5px 0">
          <el-button class="el-icon-circle-plus-outline" type="primary" @click="handleAdd" style="font-size:medium">添加</el-button>
          <el-popconfirm
                class="ml-5"
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定要批量删除吗?"
                @confirm="handleDeleteBatch">
          <el-button type="danger" slot="reference" style="font-size:medium">批量删除</el-button>
        </el-popconfirm>
        </div>

        <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg7'" @selection-change="handleSelectionChange" style="font-size:large">
          <el-table-column type="selection" width="40"></el-table-column>
          <el-table-column
            prop="id"
            label="ID"
            width="110"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="name"
            label="名称"
            width="190"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="flag"
            width="190"
            label="唯一标识"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="description"
            width="190"
            label="描述"
            align="center"
          ></el-table-column>
          <el-table-column label="操作" width="460" align="center">
            <template slot-scope="scope">
              <el-button type="info" slot="reference" @click="selectMenu(scope.row.id)">分配菜单<i class="el-icon-menu"></i></el-button>
              <el-button type="success" @click="handleEdit(scope.row)" >编辑 <i class="el-icon-edit"></i></el-button>
              <el-popconfirm
                class="ml-5"
                confirm-button-text='确定删除'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定要删除吗?"
                @confirm="handleDel(scope.row.id)"
                >
              <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <div style="padding: 10px 0">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[7, 5, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>

        <el-dialog title="角色信息" :visible.sync="dialogFormVisible" width="40%">
          <el-form label-width="80px" size="small">
            <el-form-item label="名称">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="唯一标识">
              <el-input v-model="form.flag" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="描述">
              <el-input v-model="form.description" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
          </div>
        </el-dialog>

        <el-dialog title="菜单分配" :visible.sync="menuDialogVis" width="40%">
          <el-tree
            :props="props"
            :data="menuData"
            node-key="id"
            :default-expanded-keys="expends"
            :default-checked-keys="checks"
            show-checkbox
            ref="tree"
            >
            <span class="custom-tree-node" slot-scope="{ node, data }">
              <span><i :class="data.icon"></i> {{ data.name }}</span>
            </span>
          </el-tree>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="saveRoleMenu">确 定</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
  name: 'Role',
  data(){
    return{
        tableData: [],
        total: 0,
        pageNum: 1,
        pageSize: 7,
        name: "",
        dialogFormVisible: false,
        menuDialogVis: false,
        multipleSelection: [],
        form:{},
        isEdit: false,
        menuData:[],
        props:{
          label: 'name',
        },
        expends: [],
        checks: [],
        roleId: 0
    }
  },
  created(){
    this.load();
  },
  methods:{
    load() {
      this.request.get("/role/page", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            name: this.name
          },
        }).then((res) => {
          console.log(res);
          this.tableData = res.records;
          this.total = res.total;
        });
    },
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum);
      this.pageNum = pageNum;
      this.load();
    },
    reset() {
      this.name = "",
      this.load()
    },
    handleAdd(){
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row){
      console.log('row',row);
      this.form = Object.assign({},row)
      this.dialogFormVisible = true
      this.isEdit = true
    },
    handleDel(id){
      console.log('id',id);
      this.request.delete("/role/delete/"+id).then(res =>{
        if(res.code === '200'){
          this.$message.success('删除角色成功!')
          this.load()
        }else{
          this.$message.error('删除角色失败!')
          this.load()
        }
      })
    },
    handleDeleteBatch(){
      let ids = this.multipleSelection.map(v => v.id)
      console.log('ids',ids)
      if(ids.length === 0){
        this.$message.warning('批量删除角色前,请先勾选若干角色')
      }else{
        this.request.delete("/role/deleteBatch/",{data:ids}).then(res =>{
          if(res.code === '200'){
            this.$message.success('批量删除角色成功!')
            this.load()
          }else{
            this.$message.error('批量删除角色失败!')
            this.load()
          }
        })
      }
    },
    handleSelectionChange(val){
      this.multipleSelection = val
    },
    save(){
      if(this.isEdit === false){
        console.log('add');
        this.request.post("/role/add",this.form).then(res =>{
        console.log(res)
        if(res.code === '200'){
          console.log('西瓜');
          this.$message.success('添加角色成功!')
          this.dialogFormVisible = false
          this.load()
        }else{
          this.$message.error('添加角色失败!')
          this.dialogFormVisible = false
          this.load()
        }
      })
      }else{
        console.log('edit');
        this.request.put("/role/edit",this.form).then((res) =>{
          if(res.code === '200'){
            this.$message.success("修改角色信息成功!")
            this.dialogFormVisible = false
            this.isEdit = false
            this.load()
          }else{
            this.$message.error('修改角色信息失败!')
            this.dialogFormVisible = false
            this.isEdit = false
            this.load()
          }
        })
      }
    },
    saveRoleMenu(){
      this.request.post('/role/roleMenu/'+this.roleId,this.$refs.tree.getCheckedKeys()).then(res =>{
        if(res.code === '200'){
          this.$message.success("绑定菜单成功")
          this.menuDialogVis = false
        }else{
          this.$message.error(res.msg)
        }
      })
    },
    cancel(){
      this.dialogFormVisible = false
      this.load()
    },
    selectMenu(roleId){
      this.menuDialogVis = true
      this.roleId = roleId

      //请求菜单数据
      this.request.get("/menu").then((res) => {
          this.menuData = res.data;

          this.expends = this.menuData.map(v => v.id)
        });

      this.request.get("/role/roleMenu/"+this.roleId).then((res) => {
          this.checks = res.data
        });
    }
  }
};
</script>

<style>
.headerBg7 {
  background-color: rgb(218, 220, 122) !important;
}
</style>